<?php
/*************************************************************************
Front End Functions
----------------------------------------------------------------
PICKCOCMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Last updated: 29/09/2009
**************************************************************************/
function base_url(){
	return "http://".DOMAIN;
}
function current_url() {
	$s = empty($_SERVER["HTTPS"]) ? ''
		: ($_SERVER["HTTPS"] == "on") ? "s"
		: "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
		: (":".$_SERVER["SERVER_PORT"]);
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}
/*-----------------------------------------------------------------------*
* Function: linkPager
* Parameter: URL, pages, page, open_tag, close_tag, seperator
* Return: Info object
*-----------------------------------------------------------------------*/
function linkPager($infoPages, $open_tag = '<li>', $close_tag = '</li>', $separator = ''){
	$str = '';
	if($infoPages){
		$pages = $infoPages['pages'];
		if($pages>1){
			$page = $infoPages['page'];
			$start = $page - 3;
			if($start < 1) $start = 1;
			$end = $page + 3;
			if($end > $pages) $end = $pages;
			if($page>1) $str .= $open_tag.'<a class="page" page="1" href="#First"><<</a>'.$close_tag.$separator;
			for($i = $start;$i <= $end;$i++) {
				if($i == $page) {
					$str .= $open_tag.'<strong>'.$i.'</strong>'.$close_tag.$separator;
				} else {
					$str .= $open_tag.'<a class="page" page="'.$i.'" href="#'.$i.'">'.$i.'</a>'.$close_tag;
				}
			}
			if($page<$end) $str .= $open_tag.'<a class="page" page="'.$end.'" href="#Last">>></a>'.$close_tag;
		}else{//page>1
			$str .= $open_tag.'<strong>1</strong>, '.$close_tag.$separator;
		}
	}#endif
	return $str;
}
function linkPager1($url, $infoPages = "", $open_tag = '', $close_tag = '', $separator = ''){
	$str = '';
	if($infoPages != ""){
		$pages = $infoPages['pages'];
		$page = $infoPages['page'];
		// If pages eq 1 then return
		if($pages == 1) return "";
		$start = $page - 3;
		if($start < 1) $start = 1;
		$end = $page + 3;
		if($end > $pages) $end = $pages;
		if($start > 1){
			$str .= $open_tag.'<a href="'.$url.'/p/1"><<</a>'.$close_tag.$separator;
		}
		for($i = $start; $i <= $end; $i++) {
			if($i == $page) {
				$str .= $open_tag.'<a href="javascript:;"><strong>'.$i.'</strong></a>'.$close_tag.$separator;
			} else {
				$str .= $open_tag.'<a href="'.$url.'/p/'.$i.'">'.$i.'</a>'.$close_tag;
			}
		}
		if($end < $pages){
			$str .= $open_tag.'<a href="'.$url.'/p/'.$end.'">>></a>'.$close_tag;
		}
	}#endif
	return $str;
}
// Theme
function optionTemplates($value){
	$str = '';
	$themePath = dirname(__FILE__).'/../templates/';
	if ($handle = opendir($themePath)) {

		while (false !== ($entry = readdir($handle))) {
			if(strcmp($entry, ".")!=0 && strcmp($entry, "..")!=0){
				$str .= '<option value="'.$entry.'"'.($entry==$value?' selected':'').'>'.$entry.'</option>';
			}
		}

	}
	return $str;
}
// Layout
function optionLayout($theme, $value){
	$str = '';
	$themePath = dirname(__FILE__).'/../templates/'.$theme.'/layout/';
	//die($themePath);
	if ($handle = opendir($themePath)) {

		while (false !== ($entry = readdir($handle))) {
			if(strcmp($entry, ".")!=0 && strcmp($entry, "..")!=0){
				$entry = str_replace('.temp.html', '', $entry);
				$str .= '<option value="'.$entry.'"'.($entry==$value?' selected':'').'>'.$entry.'</option>';
			}
		}

	}
	return $str;
}
function optionStatus($value = '-1'){
	$str = '';
	$str = '
	<option value="1"'.($value==1?" selected":"").'>'.l('status_text_1').'</option>	
	<option value="0"'.($value==0?" selected":"").'>'.l('status_text_0').'</option>
	<option value="2"'.($value==2?" selected":"").'>'.l('status_text_2').'</option>';
	return $str;
}

// Su ly gia tien
function sulyGia($price){
	$price = str_replace('.', '', $price);
	$price = strrev($price);
	$array = str_split($price,3);
	$price = implode(',', $array);
	return strrev($price);
}

function cutStringByWords( $string, $iWords = 32 ) 
{ 
	$iWords = intval( $iWords ) === 0 ? 32 : $iWords; 
	$aWords = explode( ' ', strip_tags($string), $iWords += 1 ); 
	if ( $iWords > sizeOf( $aWords ) ) 
		return $string; 
	array_pop( $aWords ); 
	return implode( ' ', $aWords ) . '...'; 
}

function randomArray($array, $numRandom){
    $newArray = array();
    $numArray = count($array);
    if($numArray<$numRandom) $numRandom = $numArray;
        $rand_keys = array_rand($array, $numRandom);
    foreach($rand_keys as $key){
        $newArray[] = $array[$key];
    }
    return $newArray;
    
}
function dateafter ( $a )
{
	$hours = $a * 24;
	$added = ($hours * 3600)+time();
	$month = date("m", $added);
	$day = date("d", $added);
	$year = date("Y", $added);
	$result = "$year-$month-$day";
	return $result;
}

function format_date($date, $delimiter = '/'){
	switch($delimiter){
		case '/' : 
			$arrayDate = explode($delimiter, $date);
			if(is_array($arrayDate) && count($arrayDate) == 3)
				$date = $arrayDate[2].'-'.$arrayDate[0].'-'.$arrayDate[1];
			break;
		case '-' : 
			$arrayDate = explode($delimiter, $date);
			if(is_array($arrayDate) && count($arrayDate) == 3)
				$date = $arrayDate[1].'/'.$arrayDate[2].'/'.$arrayDate[0];
			break;			

	}
	return $date;
}

function print_v($object){
    echo "<pre>";
    print_r($object);
    echo "</pre>";
}
function encodEquote($text){
	return $text;	
}
function l($key){
	global $messages;
	$key = trim($key);
	if(!empty($messages[$key])){
		return $messages[$key];	
	}
	if(!array_key_exists($key, $messages)){
		$languagesdefault = new Pick_Core_LanguagesDefault();
		$languageinfo = new Pick_Core_LanguagesDefaultInfo();
		if(!$languagesdefault->checkDataExits(array('default_text'=>$key))){
			$languageinfo->setField('default_text', $key);
			$languagesdefault->addData($languageinfo->getArrayField());
		}
		unset($languageinfo);
		unset($languagesdefault);
	}
	return $key;	
}
function redirect($url){
	if (!headers_sent($filename, $linenum)) {
		header("Location: $url");
		exit;
	} else {
		echo "Headers already sent in $filename on line $linenum\n";
		exit;
	}
}
?>